package hust.music.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import hust.music.action.base.ActionBase;
import hust.music.entity.User;
import hust.music.service.UserModService;

/**
 * RegisterAction
 * 
 * @author Txazo
 * @since 1.0
 */
@Controller
@Scope("session")
public class RegisterAction extends ActionBase {

	private static final long serialVersionUID = 1L;

	/** 用户名 */
	private String username;
	/** 昵称 */
	private String nickname;
	/** 密码 */
	private String password;

	private String rand;
	@Autowired
	private UserModService userModService;

	/**
	 * 注册处理
	 * 
	 * @return 逻辑视图
	 * @throws Exception
	 */
	public String register() throws Exception {
		String random = (String) session.get("random");
		if (!getUsername().equals("") && getUsername() != null
				&& getPassword() != null && !getPassword().equals("") && random.equals(rand)) {
			User user = new User();
			user.setUsername(username);
			user.setNickname(nickname);
			user.setAuthtoken(password);
			user.setLastAuthority(new Date());
			user.setAvatar("./images/user/default.jpg");
			User newUser = userModService.register(user);
			if (newUser != null) {
				session.put("user", newUser);
				return SUCCESS;
			}
			return INPUT;
		}
		return INPUT;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

}